package com.fenghongzhang.day009;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class MySqlHelp extends SQLiteOpenHelper {
    /**
     *
     * @param context 上下文
     * @param name  school.db
     * @param factory  null
     * @param version 1
     */
    public MySqlHelp(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    /**
     * 创建数据库  db
     * @param db
     * onCreate 只会执行一遍.
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.beginTransaction();
        db.execSQL("create table student (id integer primary key autoincrement,name varchar(20),age integer)");

//        db.execSQL("insert");
        ContentValues contentValues = new ContentValues();
        /**
         * 1,字段
         * 2.值
         */
        contentValues.put("name","小明");
        contentValues.put("age",16);
        db.insert("student",null,contentValues);
        db.setTransactionSuccessful();

        db.endTransaction();

    }

    /**
     * 更新数据库
     * @param db
     * @param oldVersion
     * @param newVersion
     * 版本变化的时候.
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
